package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/all")
    public String viewAllOrders(Model model) {
        List<Order> orderList = orderService.getOrderList();
        model.addAttribute("orderList",orderList);
        return "order/allOrder";
    }

    @GetMapping("/view")
    public String viewOrder(int orderid,Model model) {
        Order order = orderService.getOrderByOrderId(orderid);
        model.addAttribute("order",order);
        return "order/viewOrder";
    }

    @GetMapping("/edit")
    public String editOrder(int orderid,Model model) {
        Order order = orderService.getOrderByOrderId(orderid);
        orderService.updateOrder(order);
        model.addAttribute("order",order);
        return "order/editOrder";
    }

    @PostMapping("/editConfirm")
    public String editOrderConfirm(Order order, Model model, HttpServletResponse response) {
        orderService.updateOrder(order);
        model.addAttribute("order",order);
        return "redirect:/catalog/view";
    }

    @GetMapping("/orderSend")
    public String orderSend(int orderid,HttpServletResponse response) {
        Order order = orderService.getOrderByOrderId(orderid);
        orderService.setOrderReady(order);
        return "redirect:/order/all";
    }

}
